﻿using System;
using System.Xml;

namespace AnaViewer
{
    /// <summary>
    /// 表示 RSS 2.0 XML 文档中的 Item 元素。
    /// 一个 RssChannel 中包含零个或多个 RssItem。
    /// </summary>
    [Serializable]
    public class AnaItem : IItem
    {
        private readonly string title;
        private readonly string description;
        private readonly string link;
        private readonly DateTime created;
        private readonly Person person;

        public string Title { get { return title; } }
        public string Description { get { return description; } }
        public string Link { get { return link; } }
        public DateTime Created { get { return created; } }

        /// <summary>
        /// 文本格式的 创建时间。
        /// </summary>
        public string PubDate
        {
            get
            {
                string time;
                if (created == DateTime.MinValue)
                    time = "时间不详";
                else
                    time = "公元" + created.ToLongDateString();
                return time;
            }
        }

        public string Author
        {
            get
            {
                if (person == null)
                    return "";
                else
                    return person.Name;
            }
        }
        public System.Drawing.Image FaceImage
        {
            get
            {
                if (person == null)
                    return null;
                else
                    return person.Image;
            }
        }

        private string anaId;
        /// <summary>
        /// 语录的ID号
        /// </summary>
        public string AnaId { get { return anaId; } }

        public Person Person { get { return person; } }



        /// <summary>
        /// 根据表示 RSS 2.0 XML 文档内的一个 Item 元素的 XmlNode 生成一个 RSSItem。
        /// </summary>
        /// <param name="itemNode"></param>
        internal AnaItem(XmlNode itemNode)
        {
            XmlNode anaNode;
            anaNode = itemNode.SelectSingleNode("anaInfo");
            if (anaNode != null)
            {
                XmlNode selected;
                selected = anaNode.SelectSingleNode("content");
                if (selected != null)
                    title = selected.InnerText;

                selected = anaNode.SelectSingleNode("entironment");
                if (selected != null)
                    description = selected.InnerText;

                selected = anaNode.SelectSingleNode("url");
                if (selected != null)
                    link = selected.InnerText;

                selected = anaNode.SelectSingleNode("time");
                if (selected != null)
                    DateTime.TryParse(selected.InnerText.Substring(1,selected.InnerText.Length-2), out created);
                else
                    created = DateTime.MinValue;

                if(link!=null)
                    anaId = GetAnaId(link);
            }
            
            XmlNode personNode;
            personNode = itemNode.SelectSingleNode("person");
            if (personNode != null)
                person = new Person(personNode);
        }

        /// <summary>
        /// 从链接中提取语录 ID。
        /// </summary>
        /// <param name="link"></param>
        /// <returns></returns>
        string GetAnaId(string link)
        {
            string result = "";
            string baseStr = "http://www.yandui.com/yulu/";
            if (link.StartsWith(baseStr))
            {
                if (link.Length > baseStr.Length)
                    result = link.Substring(baseStr.Length, link.Length - baseStr.Length - 1);
            }

            return result;
        }
    }
    [Serializable]
    public class Person
    {
        private readonly string name;
        private readonly string imgurl;
        private readonly string sex;
        private readonly string nativeplace;
        private readonly string metier;
        private readonly string anaCount;
        /// <summary>
        /// 名字
        /// </summary>
        public string Name { get { return name; } }
        /// <summary>
        /// 头像地址
        /// </summary>
        public string ImgUrl { get { return imgurl; } }
        /// <summary>
        /// 性别
        /// </summary>
        public string Sex { get { return sex; } }
        /// <summary>
        /// 国家/区域
        /// </summary>
        public string NativePlace { get { return nativeplace; } }
        /// <summary>
        /// 行业/职务
        /// </summary>
        public string Metier { get { return metier; } }
        /// <summary>
        /// 该人物发布的语录数量。
        /// </summary>
        public string AnaCount { get { return anaCount; } }

        private System.Drawing.Image image;
        /// <summary>
        /// 头像。
        /// </summary>
        public System.Drawing.Image Image{get{return image;}}

        /// <summary>
        /// 根据表示 RSS 2.0 XML 文档内的一个 Item 元素的 XmlNode 生成一个 RSSItem。
        /// </summary>
        /// <param name="itemNode"></param>
        internal Person(XmlNode itemNode)
        {
            XmlNode selected;
            selected = itemNode.SelectSingleNode("name");
            if (selected != null)
                name = selected.InnerText;

            selected = itemNode.SelectSingleNode("imgurl");
            if (selected != null)
                imgurl = selected.InnerText;

            selected = itemNode.SelectSingleNode("sex");
            if (selected != null)
                sex = selected.InnerText;

            selected = itemNode.SelectSingleNode("nativeplace");
            if (selected != null)
                nativeplace = selected.InnerText;

            selected = itemNode.SelectSingleNode("metier");
            if (selected != null)
                metier = selected.InnerText;

            selected = itemNode.SelectSingleNode("anaCount");
            if (selected != null)
                anaCount = selected.InnerText;


            if(imgurl!=null && imgurl.Trim()!="")
                image = Common.GetImage(imgurl);
        }
    }
}